package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.util.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private JedisPool jedisPool;
    @Reference
    private OrderService orderService;
    @Reference
    private MemberService memberService;

    /**
     * 提交预约
     *
     * @param map
     * @return
     */
    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map, HttpSession session) {
       /* Jedis jedis = jedisPool.getResource();
        String telephone = (String) map.get("telephone");
        String validateCodeRedis = jedis.get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        String validateCode = (String) map.get("validateCode");
        jedis.close();
        if (validateCode != null && validateCodeRedis != null && validateCode.equals(validateCodeRedis)) {
            map.put("orderType", Order.ORDERTYPE_WEIXIN);
            Result result = null;
            try {
                result = orderService.order(map);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false, "预约失败");
            }
            if (result.isFlag()) {
                try {
                    SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE, telephone, (String) map.get("orderDate"));
                } catch (ClientException e) {
                    e.printStackTrace();
                }
            }
            return result;
        } else {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }*/
        Jedis jedis = jedisPool.getResource();
        String value =  map.get("value").toString();
        jedis.close();
        if (value != null) {
            map.put("orderType", Order.ORDERTYPE_WEIXIN);
            map.put("fam_id",value);
            Result result = null;
            Object member_id = session.getAttribute("member_id");
            map.put("member_id",Integer.parseInt(member_id.toString()));
            Member member = memberService.findById(Integer.parseInt(member_id.toString()));
            if(orderService.checkOnly(Integer.parseInt(member_id.toString()),Integer.parseInt(value),Integer.parseInt(map.get("setmealId").toString()))){
                return new Result(false, "预约失败,重复预约");
            }
            try {
                result = orderService.order(map);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false, "预约失败");
            }

          /*  if (result.isFlag()) {

                try {
                    SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE, member.getPhoneNumber(), (String) map.get("orderDate"));
                } catch (ClientException e) {
                    e.printStackTrace();
                }
            }*/
            return result;

        } else {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }


    /**
     * 预约成功返回数据
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Map map = orderService.findById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
    }
}
